package com.cdz.bus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.cdz.bus.domain.BusDockingInfo;
import com.cdz.bus.service.IBusDockingInfoService;
import com.cdz.bus.service.IRequestService;
import com.cdz.common.annotation.Excel;
import com.cdz.common.constant.ComConstants;
import com.cdz.common.constant.RequestConstants;
import com.cdz.common.exception.ServiceException;
import com.cdz.common.utils.ListUtils;
import com.cdz.common.utils.StringUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cdz.bus.mapper.BusStationMapper;
import com.cdz.bus.domain.BusStation;
import com.cdz.bus.service.IBusStationService;

/**
 * 充电站Service业务层处理
 *
 * @author guyi
 * @date 2024-11-19
 */
@Service
public class BusStationServiceImpl extends ServiceImpl<BusStationMapper, BusStation> implements IBusStationService {
    @Autowired
    private BusStationMapper busStationMapper;
    @Autowired
    private IBusDockingInfoService busDockingInfoService;
    @Autowired
    private IRequestService requestService;

    /**
     * 查询充电站
     *
     * @param stationId 充电站主键
     * @return 充电站
     */
    @Override
    public BusStation selectBusStationByStationId(String stationId) {
        return busStationMapper.selectBusStationByStationId(stationId);
    }

    /**
     * 查询充电站列表
     *
     * @param busStation 充电站
     * @return 充电站
     */
    @Override
    public List<BusStation> selectBusStationList(BusStation busStation) {
        return busStationMapper.selectBusStationList(busStation);
    }

    /**
     * 新增充电站
     *
     * @param busStation 充电站
     * @return 结果
     */
    @Override
    public int insertBusStation(BusStation busStation) {
            return busStationMapper.insertBusStation(busStation);
    }

    /**
     * 修改充电站
     *
     * @param busStation 充电站
     * @return 结果
     */
    @Override
    public int updateBusStation(BusStation busStation) {
        return busStationMapper.updateBusStation(busStation);
    }

    /**
     * 批量删除充电站
     *
     * @param stationIds 需要删除的充电站主键
     * @return 结果
     */
    @Override
    public int deleteBusStationByStationIds(String[] stationIds) {
        return busStationMapper.deleteBusStationByStationIds(stationIds);
    }

    /**
     * 删除充电站信息
     *
     * @param stationId 充电站主键
     * @return 结果
     */
    @Override
    public int deleteBusStationByStationId(String stationId) {
        return busStationMapper.deleteBusStationByStationId(stationId);
    }


    @Override
    public int syncStationData() throws Exception {
        // 查询查询对接数据
        BusDockingInfo params = new BusDockingInfo();
        params.setStatus(ComConstants.SysNormalDisable.NORMAL);
        List<BusDockingInfo> dockingList = busDockingInfoService.selectBusDockingInfoList(params);
        if (ListUtils.isEmpty(dockingList)) {
            throw new ServiceException("对接数据不存在");
        }

        for (BusDockingInfo info : dockingList) {
            JSONObject reqParams = new JSONObject();
            reqParams.put("PageNo","1");
            reqParams.put("PageSize","10");
            String vo = requestService.commonRequest(RequestConstants.QUERY_STATIONS_INFO, info, reqParams.toString());
            JSONObject voJson = JSONObject.parseObject(vo);
            JSONArray stationInfos = voJson.getJSONArray("StationInfos");
            for (Object station : stationInfos) {
                // 保存、更新站点数据
                JSONObject stationJson = (JSONObject) station;
                String stationID = stationJson.getString("StationID");
                BusStation busStation = busStationMapper.selectBusStationByStationId(stationID);
                String editFlag = "add";
                if (busStation == null)  {
                    busStation = new BusStation();
                    busStation.setStationId(stationID); // 充电站id
                }else{
                    editFlag = "edit";
                }
                busStation.setOperatorId(stationJson.getString("OperatorID"));  // 运营商id
                busStation.setEquipmentOwnerId(stationJson.getString("EquipmentOwnerID")); // 设备所属方id
                busStation.setStationName(stationJson.getString("StationName")); // 充电桩名称
                busStation.setCountryCode(stationJson.getString("CountryCode")); //充电站国家代码
                busStation.setAreaCode(stationJson.getString("AreaCode")); // 充电站省市辖区编码
                busStation.setAddress(stationJson.getString("Address")); // 详细地址
                busStation.setStationTel(stationJson.getString("StationTel")); // 站点电话
                busStation.setServiceTel(stationJson.getString("ServiceTel")); // 服务电话
                busStation.setStationType(stationJson.getLong("StationType")); //站点类型
                busStation.setStationStatus(stationJson.getLong("StationStatus")); // 站点状态
                busStation.setParkNums(stationJson.getLong("ParkNums")); //车位数量
                busStation.setStationLng(stationJson.getBigDecimal("StationLng")); // 经度
                busStation.setStationLat(stationJson.getBigDecimal("StationLat")); // 纬度
                busStation.setSiteGuide(stationJson.getString("SiteGuide")); // 站点引导(描述性文字，用于引导车主找 到充电车位)
                busStation.setConstruction(stationJson.getLong("Construction")); // 建设场所
                List<String> pictures = stationJson.getList("Pictures", String.class);
                StringBuffer sb = new StringBuffer();
                for (String picture : pictures) {
                    sb.append(picture).append(";");
                }
                busStation.setPictures(sb.substring(0,sb.length()-1)); // 站点照片
                busStation.setMatchCars(stationJson.getString("MatchCars")); // 使用车型描述
                busStation.setParkInfo(stationJson.getString("ParkInfo")); // 车位楼层及数量描述
                busStation.setBusineHours(stationJson.getString("BusineHours")); // 营业时间
                busStation.setElectricityFee(stationJson.getString("ElectricityFee")); // 充电电费率(充电费描述)
                busStation.setServiceFee(stationJson.getString("ServiceFee")); // 服务费率(服务费率描述)
                busStation.setParkFee(stationJson.getString("ParkFee")); // 停车费(停车费率描述)
                busStation.setPayment(stationJson.getString("Payment")); // 支付方式
                busStation.setSupportOrder(stationJson.getLong("SupportOrder")); // 是否支持预约
                busStation.setSyncTime(new Date()); // 数据同步时间
                if ("edit".equals(editFlag)) {
                    busStationMapper.updateBusStation(busStation);
                }else{
                    busStationMapper.insertBusStation(busStation);
                }
            }
        }
        return 0;
    }
}